﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Mtbl;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Mtbl
{
    public class mtblProductSubCatDAL:ImtblProductSubCat
    {
        HKEA_CMSEntities model;
        mtblProductSubCat subcat;
        public mtblProductSubCatDAL()
        {
            model = DataAccess.GetModelInstance();
        }
        public static mtblProductSubCatDAL GetInstance()
        {
            return new mtblProductSubCatDAL();
        }
        public int InsertENT(mtblProductSubCat ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.mtblProductSubCats.AddObject(ent);
            return model.SaveChanges();
        }

        public List<mtblProductSubCat> GetListByENT(mtblProductSubCat ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.mtblProductSubCats.Where(a => a.ProductSubCatUID > 0);
            if (ent.ProductSubCatUID > 0)
                result = result.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID);
            if (ent.ProductCatUID > 0)
                result = result.Where(a=>a.ProductCatUID==ent.ProductCatUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<mtblProductSubCat> GetListByENT(mtblProductSubCat ent)
        {
            var result = model.mtblProductSubCats.Where(a => a.ProductSubCatUID > 0);
            if (ent.ProductSubCatUID > 0)
                result = result.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID);
            if (ent.ProductCatUID > 0)
                result = result.Where(a => a.ProductCatUID == ent.ProductCatUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }

        public List<mtblProductSubCat> GetListByUIDList(List<int> list)
        {
            var result = model.mtblProductSubCats.Where(a=>list.Contains(a.ProductSubCatUID)).Where(a=>a.Deleted==0);
            return result.ToList();
        }

        public int GetCountByENT(mtblProductSubCat ent)
        {
            var result = model.mtblProductSubCats.Where(a => a.ProductSubCatUID > 0);
            if (ent.ProductSubCatUID > 0)
                result = result.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(mtblProductSubCat ent)
        {
            subcat = model.mtblProductSubCats.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID).FirstOrDefault();
            if (subcat != null)
            {
                subcat.Deleted = ent.Deleted;
                subcat.LastModifyDate = DateTime.Now;
                subcat.LastModifyBy = ent.LastModifyBy;
                subcat.ProductCatUID = ent.ProductCatUID;
                subcat.ProductSubCat_Chi = ent.ProductSubCat_Chi;
                subcat.ProductSubCat_Eng = ent.ProductSubCat_Eng;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(mtblProductSubCat ent)
        {
            subcat = model.mtblProductSubCats.Where(a => a.ProductSubCatUID == ent.ProductSubCatUID).FirstOrDefault();
            if (subcat != null)
            {
                model.mtblProductSubCats.DeleteObject(subcat);
            }
            return model.SaveChanges();
        }
    }
}
